package com.lj.luoye.lotteryticket.analysis;

import com.lj.luoye.lotteryticket.entity.LuckEntity;
import com.lj.luoye.lotteryticket.entity.Unionlotto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LuckFunc
{
    public List<LuckEntity> startlotto(List<Unionlotto> buys, Unionlotto right){
//        System.out.print("开奖号码:");
//        for(String redBall:right.getRedBalls()){
//            System.out.print(redBall+" ");
//        }
//        System.out.println("- "+right.getBlueBall());
        List<LuckEntity> luckEntityList=new ArrayList<>();


        String[] right_ball=new String[7];
        for(int i=0;i<6;i++){
            right_ball[i]=right.redBalls.get(i);
        }
        right_ball[6]=right.blueBall;

        for(Unionlotto unionlotto:buys){
            //System.out.print(unionlotto.getDate()+":");
            String[] input_ball=new String[7];
            for(int i=0;i<6;i++){
                input_ball[i]=unionlotto.redBalls.get(i);
            }
            input_ball[6]=unionlotto.blueBall;
            boolean[] goal_ball = compare_right(input_ball, right_ball);

            Unionlotto unionlotto1=new Unionlotto();
            unionlotto1.setId(unionlotto.getId());
            unionlotto1.redBalls=new ArrayList<>();
            for (int i = 0; i < goal_ball.length; i++)
            {
                //System.out.print(goal_ball[i]+",");
                if (goal_ball[i])
                {
                    if (i < 6)
                    {
                        unionlotto1.redBalls.add(input_ball[i]);
                    }
                    else {
                        unionlotto1.blueBall=input_ball[i];
                    }
                }
            }

            //中奖结果
            String result = judge_goal(goal_ball);

            luckEntityList.add(new LuckEntity(unionlotto1,result));
            unionlotto.setLuckResult(result);
            //打印中奖结果
//            System.out.print("中奖号码:");
//            for(String redBall:unionlotto1.getRedBalls()){
//                System.out.print(redBall+" ");
//            }
//            System.out.println("- "+unionlotto1.getBlueBall());
//            System.out.println("中奖结果："+result);
        }
        return luckEntityList;
    }

    public List<LuckEntity> startlottoMulti(List<Unionlotto> buys, Unionlotto right){
        List<LuckEntity> luckEntityList=new ArrayList<>();
        for(Unionlotto unionlotto:buys){
            //比较红球
            String[] input_red_ball=new String[unionlotto.redBalls.size()];
            for(int i=0;i<unionlotto.redBalls.size();i++){
                input_red_ball[i]=unionlotto.redBalls.get(i);
            }
            String[] right_ball=new String[6];
            for(int i=0;i<6;i++){
                right_ball[i]=right.redBalls.get(i);
            }
            boolean[] red_ball = compare_right_s(input_red_ball, right_ball);

            //比较蓝球
            String[] input_blue_ball=new String[unionlotto.blueBalls.size()];
            for(int i=0;i<unionlotto.blueBalls.size();i++){
                input_blue_ball[i]=unionlotto.blueBalls.get(i);
            }
            right_ball=new String[1];
            right_ball[0]=right.blueBall;
            boolean[] blue_ball = compare_right_s(input_blue_ball, right_ball);

            Unionlotto unionlotto1=new Unionlotto();
            unionlotto1.setId(unionlotto.getId());
            unionlotto1.redBalls=new ArrayList<>();
            for (int i = 0; i < red_ball.length; i++)
            {
                if (red_ball[i])
                {
                    unionlotto1.redBalls.add(input_red_ball[i]);
                }
            }
            unionlotto1.blueBalls=new ArrayList<>();
            for (int i = 0; i < blue_ball.length; i++)
            {
                if (blue_ball[i])
                {
                    unionlotto1.blueBalls.add(input_blue_ball[i]);
                }
            }
            luckEntityList.add(new LuckEntity(unionlotto1,""));

            List<Unionlotto> expand = unionlotto.expand();
            startlotto(expand, right);
            boolean lucky=false;
            Map<String,Integer> luckMap=new HashMap<>();
            for(Unionlotto e:expand){
                if(e.luckResult.contains("等奖")){
                    Integer count = luckMap.get(e.luckResult);
                    if(count==null){
                        luckMap.put(e.luckResult,1);
                    }else{
                        count+=1;
                        luckMap.put(e.luckResult,count);
                    }
                    lucky=true;
                }
            }
            if(lucky){
                unionlotto.luckResult="";
                for(String key:luckMap.keySet()){
                    unionlotto.luckResult+=key+"*"+luckMap.get(key)+" ";
                }
            }else {
                unionlotto.luckResult="未获奖";
            }
        }
        return luckEntityList;
    }

    //判断最后输入的值是否中奖
    public boolean[] compare_right(String[] input_ball, String[] right_choose)
    {
        boolean[] right_ball = new boolean[7];

        //判断蓝色球是否正确
        if (input_ball[6].equals(right_choose[6]))
        {
            right_ball[6] = true;
        }
        else
        {
            right_ball[6] = false;
        }
        for (int i = 0; i < input_ball.length - 1; i++)
        {
            for (int j = 0; j < right_choose.length - 1; j++)
            {
                if (input_ball[i].equals(right_choose[j]))
                {
                    right_ball[i] = true;
                    break;
                }
                else
                {
                    right_ball[i] = false;
                }
            }
        }
        return right_ball;
    }

    public boolean[] compare_right_s(String[] input_ball, String[] right_choose)
    {
        boolean[] right_ball = new boolean[input_ball.length];
        for (int i = 0; i < input_ball.length; i++)
        {
            for (int j = 0; j < right_choose.length; j++)
            {
                if (input_ball[i].equals(right_choose[j]))
                {
                    right_ball[i] = true;
                    break;
                }
                else
                {
                    right_ball[i] = false;
                }
            }
        }
        return right_ball;
    }

    //判断获取几等奖,根据网上规则
    public String judge_goal(boolean[] input_goal)
    {
        int red_count = 0;
        int blue_count = 0;
        for (int i = 0; i < input_goal.length - 1; i++)
        {
            if (input_goal[i])
            {
                red_count++;
            }
        }
        if (input_goal[input_goal.length - 1])
        {
            blue_count = 1;
        }
        switch (red_count)
        {
            case 0:
            case 1:
                //return "六等奖";
            case 2:
                if (blue_count == 1)
                {
                    return "六等奖";
                }
                else
                {
                    return "未获奖";
                }
            case 3:
                if (blue_count == 1)
                {
                    return "五等奖";
                }
                else
                {
                    return "未获奖";
                }
            case 4:
                if (blue_count == 1)
                {
                    return "四等奖";
                }
                else
                {
                    return "五等奖";
                }
            case 5:
                if (blue_count == 1)
                {
                    return "三等奖";
                }
                else
                {
                    return "四等奖";
                }
            case 6:
                if (blue_count == 1)
                {
                    return "一等奖";
                }
                else
                {
                    return "二等奖";
                }
            default:
                return "未获奖";
        }
    }
}
